<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\User;

use App\Controller\Admin\AdminController;
use App\Model\Order;
use App\Repositories\OrderRepository;
use App\Repositories\UserAccountRepository;
use App\Repositories\UserRepository;
use App\Repositories\UserWechatRepository;
use App\Services\AccountService;
use App\Services\UserService;
use Hyperf\Di\Annotation\Inject;

class UserController extends AdminController
{
    /**
     * @Inject
     * @var AccountService
     */
    private $service;

    /**
     * @Inject
     * @var UserService
     */
    private $user_service;

    public function list(){
        $rows = $this->request->input('rows',10);
        $start_time = $this->request->input('start_time','');
        $end_time = $this->request->input('end_time','');
        $mobile = $this->request->input('mobile','');
        $wx_nickname = $this->request->input('wx_nickname','');
        $join=[];
        $search=[];
        $search['is_virtual']=[
            'field' => 'is_virtual',
            'value' => 0
        ];
        if($start_time!=''){
            $search['start_time']=[
                'field' => 'reg_time',
                'value' => $start_time,
                'method' => '>='
            ];
        }
        if($end_time!=''){
            $search['end_time']=[
                'field' => 'reg_time',
                'value' => $end_time,
                'method' => '<='
            ];
        }
        if($mobile!=''){
            $search['mobile']=[
                'field' => 'mobile',
                'value' => $mobile,
                'method' => 'like'
            ];
        }
        if($wx_nickname!=''){
            $join = [
                'user_wechat' => [
                    'on' => ['users.id','=','user_wechat.user_id'],
                    'where'=>[[
                        'field' => 'wx_nickname',
                        'value' => $wx_nickname,
                        'method' => 'like'
                    ]]
                ]
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $column=[
            'users.*'
        ];
        $datas = UserRepository::getInstance()->getList($column,$search,['limit'=>$rows],$orderBy,$join);
        if($datas['result']){
            foreach ($datas['result'] as &$item){
                $item['account'] = UserAccountRepository::getInstance()->one('*',[[
                    'field' => 'user_id',
                    'value' => $item['id']
                ]]);
                $item['wechat'] = UserWechatRepository::getInstance()->one('*',[[
                    'field' => 'user_id',
                    'value' => $item['id']
                ]]);
            }
        }
        return $this->response->success($datas);
    }

    public function detail(){
        $id = $this->request->input('id');
        $user = UserRepository::getInstance()->getById($id);
        $user['wechat'] = $user->wechat;
        $user['account'] = $user->account;
        return $this->response->success($user);
    }

    public function delete(){
        $id = $this->request->input('id');
        $return = $this->user_service->delete($id);
        if($return['success']){
            return $this->response->success($return['data']);
        }
        return $this->response->error(10010,$return['msg']);
    }
}